package com.example.yam.greenapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 杨建利 on 2017/2/3.
 */

public class ShdzAdapter extends ArrayAdapter<Shdz> {
    private int resourceId;
    private List<Shdz> shdzList = new ArrayList<Shdz>();
    public ShdzAdapter(Context context, int textViewResourceId, List objects) {
        super(context,textViewResourceId, objects);
        this.resourceId=textViewResourceId;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Shdz shdz=getItem(position);
        View view;
        ViewHoder viewHoder;
        //创建ImageLoader,用于将图片存入缓存和从缓存中取出图片
        ImageLoader mImageLoader= MyApplication.getmImageLoader();
        if(convertView == null) {//如果没有进行过布局

            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            viewHoder = new ViewHoder();

            viewHoder.name = (TextView) view.findViewById(R.id.shdz_receiver);
            viewHoder.address = (TextView) view.findViewById(R.id.shdz_address);
            viewHoder.rd = (RadioButton) view.findViewById(R.id.shdz_check);
            //将viewHoder存储在view的Tag中
            view.setTag(viewHoder);
        }else {
            view = convertView;
            viewHoder = (ViewHoder) view.getTag();//重新获取viewhoder
        }
        viewHoder.name.setText(shdz.getName());
        viewHoder.address.setText(shdz.getPhone()+"||"+shdz.getLocation());
        viewHoder.rd.setChecked(shdz.isSelect());
        viewHoder.rd.setEnabled(shdz.isSelect());
        return view;
    }
    public void setList(List<Shdz> shdzList){
        this.shdzList = shdzList;
    }
    public void Select(int position){
        for(int i=0,len=this.shdzList.size();i<len;i++){
            this.shdzList.get(i).setSelected(false);
        }
        this.shdzList.get(position).setSelected(true);
        notifyDataSetChanged();
    }
    class ViewHoder{//缓存控件
        TextView name;//名称
        TextView address;//地址
        RadioButton rd;//选择框
    }

}
